<?php
/**
 * CONTENIDO extension pack (mpContenidoX), consists of multiple classes to
 * provide more features for Plugins/Modules.
 *
 * @package     mpContenidoX
 * @subpackage  PluginInstaller
 * @author      Murat Purc <murat@purc.de>
 * @copyright   Copyright (c) 2008-2012 Murat Purc (http://www.purc.de)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 * @version     $Id: Settings.class.php 21 2012-07-16 21:33:14Z purcaholic $
 */


defined('CON_FRAMEWORK') or die('Illegal call');


/**
 * Plugin plugin installer settings class.
 *
 * @package     mpContenidoX
 * @subpackage  PluginInstaller
 * @author      Murat Purc <murat@purc.de>
 * @copyright     2009 Murat Purc
 */
class mpPluginInstaller_Settings implements mpPluginInstaller_SettingsInterface
{
    protected $_cfg;
    protected $_installSql = null;
    protected $_updateSql  = null;
    protected $_uninstallSql  = null;
    protected $_info = array();


    public function __construct($pluginDir)
    {
        if (!is_dir($pluginPath)) {
            throw new Exception("Invalid dir parameter $pluginDir");
        }

        $this->dir = $pluginDir;

        $this->getInstallSql();

        $this->_extractPluginInfo();
    }


    /**
     * Returns install sql instance
     * @return  mpPluginInstaller_Sql
     */
    public function getInstallSql()
    {
        if ($this->_installSql == null) {
            $this->_installSql = new mpPluginInstaller_Sql($this->_getInstallerSql('install'));
        }
        return $this->_installSql;
    }


    /**
     * Returns update sql instance
     * @return  mpPluginInstaller_Sql
     */
    public function getUpdateSql()
    {
        if ($this->_updateSql == null) {
            $this->_updateSql = new mpPluginInstaller_Sql($this->_getInstallerSql('update'));
        }
        return $this->_updateSql;
    }


    /**
     * Returns uninstall sql instance
     * @return  mpPluginInstaller_Sql
     */
    public function getUninstallSql()
    {
        if ($this->_uninstallSql == null) {
            $this->_uninstallSql = new mpPluginInstaller_Sql($this->_getInstallerSql('uninstall'));
        }
        return $this->_uninstallSql;
    }


    public function __set($var, $val)
    {
        $this->_info[$var] = $val;
    }


    public function __get($var)
    {
        return (isset($this->_info[$var])) ? $this->_info[$var] : null;
    }


    public function __isset($var)
    {
        return (isset($this->_info[$var]));
    }


    public function __unset($var)
    {
        if (isset($this->_info[$var])) {
            unset($this->_info[$var]);
        }
    }


    protected function _getInstallerSql($mode)
    {
        if ($mode == 'install') {
            $file = $this->dir . 'install.sql';
        } elseif ($mode == 'update') {
            $file = $this->dir . 'update.sql';
        } elseif ($mode == 'uninstall') {
            $file = $this->dir . 'uninstall.sql';
        } else {
            $file = '';
        }
        if (!empty($file) && is_file($file)) {
            return file_get_contents($file);
        } else {
            return '';
        }
    }


    protected function _extractPluginInfo()
    {
        $rawSqL = $this->_installSql->getRawSql();
        if (preg_match('/####(.*)####/i', $rawSqL, $matches)) {
            $matches = explode(';', $matches[1]);
            // take some nice names easier to work with...
            $this->name       = $matches[0];
            $this->version    = $matches[1];
            $this->author     = $matches[2];
            $this->idinternal = $matches[3];

            unset($matches);
        } else {
            throw new Exception(
                'Info missing. First line of install.sql should include following data:' . "\n"
              . '<strong>####NAME;VERSION;AUTHOR;INTERNAL_ID####</strong>'  . "\n"
              . 'No further action takes place'
            );
        }
    }

}
